package com.example.smartagriculture.config;

import com.example.smartagriculture.handler.NettyServerHandler;
import com.example.smartagriculture.service.IMeteorologyMsgService;
import com.example.smartagriculture.utils.DecoderUtil;
import com.example.smartagriculture.utils.EncoderUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Configuration
public class NettyServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Resource
    private NettyServerHandler nettyServerHandler;


    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        //接收消息格式,使用自定义解析数据格式
        pipeline.addLast("decoder",new DecoderUtil());
        //发送消息格式，使用自定义解析数据格式
        pipeline.addLast("encoder",new EncoderUtil());
        System.out.println("here");
        //针对客户端，如果在1分钟时没有想服务端发送写心跳(ALL)，则主动断开
        //如果是读空闲或者写空闲，不处理,这里根据自己业务考虑使用
        //pipeline.addLast(new IdleStateHandler(600,0,0, TimeUnit.SECONDS));
        //自定义的空闲检测
        pipeline.addLast(nettyServerHandler);
    }
}
